package io.renren.modules.ledger.service.impl;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.ledger.dao.RecordDao;
import io.renren.modules.ledger.entity.RecordEntity;
import io.renren.modules.ledger.service.RecordService;


@Service("recordService")
public class RecordServiceImpl extends ServiceImpl<RecordDao, RecordEntity>
        implements RecordService {

    @Autowired
    private RecordDao dao;

    /**
     * 单表分页查询
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<RecordEntity> page = this.selectPage(
                new Query<RecordEntity>(params).getPage(),
                new EntityWrapper<RecordEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 分页查询
     */
    @Override
    public PageUtils list4Page(Map<String, Object> params){
        Page<RecordEntity> page = new Query<RecordEntity>(params).getPage();
        return new PageUtils(page.setRecords(dao.list4Page(page, params)));
    }

    /**
     * 条件查询
     */
    @Override
    public List<RecordEntity> listByCondition(Map<String, Object> params) {
        return dao.listByCondition(params);
    }

    /**
     * 逻辑删除多条记录
     */
    @Override
    public Boolean deleteLogic(List ids) {
        return dao.deleteLogic(ids);
    }

    /**
     * 根据id查询详情
     */
    @Override
    public RecordEntity queryOneById(Long id) {
        return dao.queryOneById(id);
    }

    @Override
    public R sync(RecordEntity entity) {
        // 1 提交移动端数据
        if(entity == null || entity.getLog() == null || entity.getLog().equals("")){
            return R.ok();
        }
        List<RecordEntity> list = JSON.parseArray(entity.getLog(), RecordEntity.class);
        if(list != null && list.size() > 0){
            this.insertOrUpdateBatch(list);
        }

        // 2 查询系统数据
        if(entity.getUserId() == null || entity.getUserId().equals("")){
            return R.ok();
        }
        List<Map> recordList = dao.listForSync(entity);
        return R.ok().put("recordList", recordList);
    }



}
